<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>图书管理</title>
    <%@include file="/pages/common/head.jsp" %>

    <script type="text/javascript">
        $(function () {
            $(".deleteclass").click(function () {
                return confirm("确定要删除【" + $(this).parent().parent().find("td:first").text() + "】吗?");
            })

            $(".updateclass").click(function () {
                return confirm("确定要修改【" + $(this).parent().parent().find("td:first").text() + "】吗?");
            })

            $("#btn").click(function () {
                var val = $("#pn_input").val();
                if (1 <= val && val <=${requestScope.page.pageTotal}) {
                    return true;
                } else if (val >${requestScope.page.pageTotal}) {
                    alert("输入页号大于最大页号，自动跳转到末页！");
                } else if (val < 1) {
                    alert("输入页号小于最小页号，自动跳转到首页！");
                }
            })
        })
    </script>
</head>
<body>

<div id="header">
    <img class="logo_img" alt="" src="static/img/logo.gif">
    <span class="wel_word">图书管理系统</span>
    <%@include file="/pages/common/manager_menu.jsp" %>
</div>

<div id="main">
    <table>
        <tr>
            <td>名称</td>
            <td>价格</td>
            <td>作者</td>
            <td>销量</td>
            <td>库存</td>
            <td colspan="2">操作</td>
        </tr>
        <c:forEach items="${requestScope.page.items}" var="book">
            <tr>
                <td>${book.name}</td>
                <td>${book.price}</td>
                <td>${book.author}</td>
                <td>${book.sales}</td>
                <td>${book.stock}</td>
                <td><a class="updateclass"
                       href="manager/bookServlet?action=getBook&id=${book.id}&pageNum=${requestScope.page.pageNum}">修改</a>
                </td>
                <td><a class="deleteclass" href="manager/bookServlet?action=delete&id=${book.id}">删除</a></td>
            </tr>
        </c:forEach>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td><a href="pages/manager/book_add.jsp?pageNum=${requestScope.page.pageTotal}">添加图书</a></td>
        </tr>
    </table>

    <%--处理分页--%>
    <div id="page_nav">
        <a href="manager/bookServlet?action=paging&pageNum=${1}">首页</a>

        <c:if test="${requestScope.page.pageNum>1}">
            <a href="manager/bookServlet?action=paging&pageNum=${requestScope.page.pageNum-1}">上一页</a>
        </c:if>

        <c:choose>
            <c:when test="${requestScope.page.pageTotal<=5}">
                <c:forEach begin="1" end="${requestScope.page.pageTotal}" var="i">
                    <c:if test="${i!=requestScope.page.pageNum}">
                        <a href="manager/bookServlet?action=paging&pageNum=${i}"> ${i}</a>
                    </c:if>
                    <c:if test="${i==requestScope.page.pageNum}">
                        <<${i}>>
                    </c:if>
                </c:forEach>
            </c:when>

            <c:when test="${requestScope.page.pageTotal>5}">
                <c:choose>
                    <c:when test="${requestScope.page.pageNum<=3}">
                        <c:forEach begin="1" end="5" var="i">
                            <c:if test="${i!=requestScope.page.pageNum}">
                                <a href="manager/bookServlet?action=paging&pageNum=${i}">${i}</a>
                            </c:if>
                            <c:if test="${i==requestScope.page.pageNum}">
                                <<${i}>>
                            </c:if>
                        </c:forEach>
                    </c:when>
                    <c:when test="${requestScope.page.pageNum>requestScope.page.pageTotal-3}">
                        <c:forEach begin="${requestScope.page.pageTotal-4}" end="${requestScope.page.pageTotal}"
                                   var="i">
                            <c:if test="${i!=requestScope.page.pageNum}">
                                <a href="manager/bookServlet?action=paging&pageNum=${i}">${i}</a>
                            </c:if>
                            <c:if test="${i==requestScope.page.pageNum}">
                                <<${i}>>
                            </c:if>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <c:forEach begin="${requestScope.page.pageNum-2}" end="${requestScope.page.pageNum+2}"
                                   var="i">
                            <c:if test="${i!=requestScope.page.pageNum}">
                                <a href="manager/bookServlet?action=paging&pageNum=${i}">${i}</a>
                            </c:if>
                            <c:if test="${i==requestScope.page.pageNum}">
                                <<${i}>>
                            </c:if>
                        </c:forEach>
                    </c:otherwise>
                </c:choose>
            </c:when>
        </c:choose>

        <c:if test="${requestScope.page.pageNum<requestScope.page.pageTotal}">
            <a href="manager/bookServlet?action=paging&pageNum=${requestScope.page.pageNum+1}">下一页</a>
        </c:if>
        <a href="manager/bookServlet?action=paging&pageNum=${requestScope.page.pageTotal}">末页</a>

        共${requestScope.page.pageTotal}页，${requestScope.page.pageTotalCount}条记录
        <br/>
        <form method="post" action="manager/bookServlet">
            到第
            <input type="hidden" name="action" value="forpaging">
            <input value="${requestScope.pn}" name="pn" id="pn_input"/>页
            <input id="btn" type="submit" value="确定">
        </form>
    </div>
</div>

<%@include file="/pages/common/foot.jsp" %>
</body>
</html>